package cn.edu.ujn.lab3.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.CustomerQueryVo;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.IBaseDictService;
import cn.edu.ujn.lab3.service.ICustomerService;
import cn.edu.ujn.lab3.service.impl.CustomerServiceImpl;
import cn.edu.ujn.lab3.util.Page;

/**
 * 客户控制器类
 */
@Controller
@RequestMapping(value = "/customer")
public class CustomerController { 
	// 注入IBaseDictService
	@Autowired
	private IBaseDictService baseDictService;
	// 注入ICustomerService
	@Autowired
	private ICustomerService customerService  ;

	/**
	 * 用户登录  
	 */
	@RequestMapping(value = "/list.action")
	public String findAllCustomer(CustomerQueryVo vo, Model model) {

		// 查询客户信息来源  
		List<BaseDict> fromType = baseDictService.selectByTypeCode("002");   
		// 将数据添加到model中
		model.addAttribute("fromType", fromType);
		// 查询客户所属行业
		List<BaseDict> industryType = baseDictService.selectByTypeCode("001");
		// 将数据添加到model中
		model.addAttribute("industryType", industryType);
		// 查询客户所属级别
		List<BaseDict> levelType = baseDictService.selectByTypeCode("006");
		// 将数据添加到model
		model.addAttribute("levelType", levelType);

		// 默认当前页为
		if (vo.getPage() == null) {
			vo.setPage(1);
		}
		// 设置查询的起始记录条数
		vo.setStart((vo.getPage() - 1) * vo.getSize());

		// 查询数据列表和数据总数
		List<Customer> resultList = customerService.findCustomerByVo(vo);
		Integer count = customerService.findCustomerByVoCount(vo);

		// page是dao中的类，将查询结果封装到该类中
		Page<Customer> page = new Page<Customer>();
		page.setTotal(count); // 数据总数
		page.setSize(vo.getSize());// 每页显示条数
		page.setPage(vo.getPage());// 当前页数
		page.setRows(resultList); // 数据列表
		// 将查询结果返回给页面
		model.addAttribute("page", page);

		// QueryVo中封装了custName、custSource、custIndustry、custLevel这几个属性
		// 高级查询选中数据回显
		model.addAttribute("custName", vo.getCustName());
		model.addAttribute("custSource", vo.getCustSource());
		model.addAttribute("custIndustry", vo.getCustIndustry());
		model.addAttribute("custLevel", vo.getCustLevel()); 

		return "customer"; 
	}

	// 新建客户功能
	@RequestMapping(value = "/create.action") 
	@ResponseBody
	public String createCustomer(Customer record, HttpServletRequest req) {

		User user = (User) req.getSession().getAttribute("USER_SESSION"); // 获取session中的User对象
		record.setCustCreateId(user.getUserId());
		System.out.println(user.getUserId());
		Date aa = new Date();
		record.setCustCreatetime(aa);
		int insert = customerService.insert(record); // 调用Service层
		if (insert > 0)
			return "OK";
		return null;
	}

	// 删除客户功能
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteByPrimaryKey(Integer id) {
		Integer a = customerService.deleteByPrimaryKey(id);
		if (a != null) {
			return "OK";
		} else {
			return " ";
		}
	}

	@ResponseBody
	@RequestMapping("/getCustomerById.action")
	public Customer getCustomerById(Long id) {
		// 通过ID查找用户信息
		return customerService.getCustomerById(id);
	}

	@ResponseBody
	@RequestMapping("/update.action") 
	public String updateCustomer(Customer customer) { 
		// 通过ID修改用户信息
		// 1.信息不合法 
		System.out.println(customer); 
		if (customerService.checkCustomer(customer)) { 
			int number = customerService.upCustomerById(customer); 
			if (number > 0) { 
				// 2.修改成功 
				return "OK"; 
			} else { 
				// 3.修改失败 
				return "NO"; 
			} 
		} else { 
			return "FALSE"; 
		} 
	}
}
